package com.cn.web;


import com.alibaba.fastjson.JSON;
import com.cn.pojo.Brand;
import com.cn.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    private BrandService service = new BrandService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //h获取请求体数据
        BufferedReader reader = req.getReader();
        String params = reader.readLine();
        System.out.println(params);

        //将JSON字符串转换为Java对象
        Brand brand = JSON.parseObject(params, Brand.class);

        //调用service完成添加
        service.add(brand);

        //3. 响应成功标识
        resp.getWriter().write("success");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
